import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    gwclist: []
  },
  mutations: {
    /* 点击加入购物车，添加数据到gwclist */
    add(state, item) {
      var gwclist = state.gwclist.find(it => {
        console.log(it.id, item.id);
        return it.id === item.id
      })
      if (gwclist) {
        gwclist.count++
      } else {
        Vue.set(item,'count',1)
         state.gwclist.push(item)
      } 
    },
    /* 点击购物车数量-- */
    djrem(state,id){
      var addcount =state.gwclist.find(it => {
        return it.id === id
      })
      if(addcount&&addcount.count>1){
        addcount.count--
      }else{
        var remcount =state.gwclist.findIndex(it => {
          return it.id === id
        })
        state.gwclist.splice(remcount,1)
      }
      
    },
    /* 点击购物车数量++ */
    djadd(state,id){
     var addcount =state.gwclist.find(it => {
        return it.id === id
      })
      if(addcount){
        addcount.count++
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
